<?php

require 'vendor/autoload.php';

use QL\QueryList;

//第一步 采集到信息
//采集某页面所有的图片
$url = 'http://www.techweb.com.cn/shoujiyouxi/list_3.shtml#wp';
$range = '.list_con .picture_text';//找的哪个元素
//定义规则
$rules = [
    //标题的规则
    'title' => [
        '.text  a','text'
    ],
    // 图片的规则
    'image' => [
        '.picture a > img','src'
    ],
    //获取日期
    'create_at' => [
        '.text .time_tag span:eq(0)','text'
    ],
    // 链接
    'link' => [
        '.text a','href'
    ],
];
// queryData 获取数据
$data = QueryList::get($url)->rules($rules)->range($range)->queryData();
//打印结果
//print_r($data);die();

//获取详情
foreach ($data as $key => $value) {
    //二次采集、详情链接
    $content = QueryList::get($value['link'])->find("#content")->text();
    $data[$key]['content'] =$content;
}

//第二步、采集到的数据保存入库
// 连接数据库
$pdo = new PDO("mysql:host=127.0.0.1;dbname=1910",'root','password');
//书写sql语句
$sql = "INSERT INTO `games` VALUES (NULL,?,?,?,?,?)";

// 预处理语句
$obj = $pdo->prepare($sql);
foreach ($data as $value) {
    $obj->bindParam("1",$value['title']);
    $obj->bindParam("2",$value['image']);
    $obj->bindParam("3",$value['create_at']);
    $obj->bindParam("4",$value['content']);
    $obj->bindParam("5",$value['link']);
    $res = $obj->execute();//执行、入库
}






